package com.datastructure;

import java.util.LinkedList;

public class BinaryTreeBianLi
{
    /**
     * 二叉树结点
     */
//    private static class TreeNode
//    {
//        int data;
//        TreeNode leftNode;
//        TreeNode rightNode;
//
//        TreeNode(int data)
//        {
//            this.data = data;
//        }
//    }

    //创建二叉树
    public static TreeNode createBinaryTree(LinkedList<Integer> inputList)
    {
        TreeNode node = null;
        if (inputList == null || inputList.isEmpty())
        {//LinkedList判空方法
            return null;
        }
        Integer data = inputList.removeFirst();//不使用int，JDK自带的LinkedList的removeFirst()方法，获取并返回第1个元素
        if (data != null)
        {
            node = new TreeNode(data);
            node.leftNode = createBinaryTree(inputList);
            node.rightNode = createBinaryTree(inputList);
        }
        return node;//控制null,控制头结点（返回根）,控制出口
    }

    //先序遍历
    public static void preOutPrint(TreeNode node)
    {
        if (node == null)
        {
            return;
        }
        System.out.println(node.data);
        preOutPrint(node.leftNode);
        preOutPrint(node.rightNode);
    }

    //中序遍历
    public static void minOutPrint(TreeNode node)
    {
        if (node == null)
        {
            return;
        }
        minOutPrint(node.leftNode);
        System.out.println(node.data);
        minOutPrint(node.rightNode);
    }

    //后序遍历
    public static void lastOutPrint(TreeNode node)
    {
        if (node == null)
        {
            return;
        }
        lastOutPrint(node.leftNode);
        lastOutPrint(node.rightNode);
        System.out.println(node.data);
    }
}
